package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 课程预习 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/sCatalogPreview")
public class SCatalogPreviewController {

    @Autowired
    private ISCatalogPreviewService catalogPreviewService;
    @Autowired
    private ISCatalogPreviewSynService catalogPreviewSynService;
    @Autowired
    private ITClassService classService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ISPreviewClassService previewClassService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;

    /**
     *  查看班级
     */
    @GetMapping("/answerClass")
    public ApiReturnObject answerClass(Integer catalogId ){
        if(catalogId != null){
            List<TClass> classList = classService.selectTClassByCatalogIdPreviewAll(catalogId);
            if(classList != null && classList.size() > 0){
                List<TClass> dept = findDept(classList);
                return new ApiReturnObject("200","查询成功",dept);
            }
            return new ApiReturnObject("200","结果为空",classList);
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /**
     *  修改查看班级
     */
    @PostMapping("/updateClass")
    public ApiReturnObject updateClass(@RequestBody SCatalogPreview preview){
        //catalogId             classList
        if(preview.getCatalogId() != null ){
            SPreviewClass previewClass = new SPreviewClass();
            previewClass.setCatalogId(preview.getCatalogId());
            //先删除中间表数据
            previewClassService.deleteSPreviewClassByCatalogId(preview.getCatalogId());
            if(preview.getClassList() != null && preview.getClassList().size() > 0){
                previewClass.setCreateTime(new Date());
                for (TClass tClass : preview.getClassList()) {
                    previewClass.setClassId(tClass.getId());
                    previewClassService.insertSPreviewClass(previewClass);
                }
            }
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }


    /**
     *  查看内容 针对于左侧目录  1
     */
    @GetMapping("/ansContent")
    public ApiReturnObject ansContent(Integer catalogId){
        //查出该目录下的所有课程问答     将选中的属性设置为true
        if(catalogId != null ){
            //全部内容集合
            SCatalogPreview preview = new SCatalogPreview();
            preview.setCatalogId(catalogId);
            List<SCatalogPreview> answeringList = catalogPreviewService.selectSCatalogPreviewList(preview);
            //选中内容集合
            List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewByCatalogId(catalogId);
            if(previews != null && previews.size() > 0){
                for (SCatalogPreview sCatalogPreview : previews) {
                    for (SCatalogPreview catalogPreview : answeringList) {
                        if(sCatalogPreview.getId().equals(catalogPreview.getId())){
                            catalogPreview.setChecked(true);
                        }
                    }
                }
            }
            return new ApiReturnObject("200","查询成功",answeringList);
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /** 修改查看内容 */
    @PostMapping("/updateContent")
    public ApiReturnObject updateContent(@RequestBody SCatalogPreview preview){
        //catalogId            classList
        if(preview.getCatalogId() != null ){
            SCatalogPreviewSyn previewSyn = new SCatalogPreviewSyn();
            previewSyn.setCatalogId(preview.getCatalogId());
            List<SCatalogPreviewSyn> sCatalogPreviewSyns = catalogPreviewSynService.selectSCatalogPreviewSynList(previewSyn);
            //中间表中存在数据
            if(sCatalogPreviewSyns != null && sCatalogPreviewSyns.size() > 0){
                // 改变往中间添加的数据的状态
                SCatalogPreview preview1 = catalogPreviewService.selectSCatalogPreviewById(sCatalogPreviewSyns.get(0).getPreviewId());
                //根据中间表id 获取数据
                if(preview1 != null){
                    //选中内容的id集合     传过来的是id集合
                    if(preview.getAnsweringList() != null && preview.getAnsweringList().size() > 0 ){
                        //新建一个集合
                        ArrayList<Integer> list = new ArrayList<>();
                        for (SCatalogPreview sCatalogPreview : preview.getAnsweringList()) {
                            //修改状态
                            sCatalogPreview.setIsStu(preview1.getIsStu());
                            sCatalogPreview.setSynch(preview1.getSynch());
                            catalogPreviewService.updateSCatalogPreview(sCatalogPreview);
                            //查看内容的id
                            list.add(sCatalogPreview.getId());
                        }
//                      //没有选择了查看内容 将状态重置为 0 0
                        SCatalogPreview catalogPreview = new SCatalogPreview();
                        catalogPreview.setCatalogId(preview.getCatalogId());
                        //该目录全部预习
                        List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewList(catalogPreview);
                        for (SCatalogPreview sCatalogPreview : previews) {
                            //如果查看内容里不包括当前内容 修改其状态
                            if(!list.contains(sCatalogPreview.getId())){
                                SCatalogPreview preview2 = catalogPreviewService.selectSCatalogPreviewById(sCatalogPreview.getId());
                                preview2.setSynch("0");
                                preview2.setIsStu("0");
                                catalogPreviewService.updateSCatalogPreview(preview2);
                            }
                        }
                        //修改查看班级
                        previewClassService.deleteSPreviewClassByCatalogId(preview.getCatalogId());
                        if(preview.getClassList().size() > 0){
                            SPreviewClass sPreviewClass = new SPreviewClass();
                            sPreviewClass.setCatalogId(preview.getCatalogId());
                            for (TClass tClass : preview.getClassList()) {
                                sPreviewClass.setClassId(tClass.getId());
                                sPreviewClass.setCreateTime(new Date());
                                previewClassService.insertSPreviewClass(sPreviewClass);
                            }
                        }

                    }else {
                        //没有选择了查看内容 将状态重置为 0 0
                        SCatalogPreview catalogPreview = new SCatalogPreview();
                        catalogPreview.setCatalogId(preview.getCatalogId());
                        List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewList(catalogPreview);
                        for (SCatalogPreview sCatalogPreview : previews) {
                            sCatalogPreview.setIsStu("0");
                            sCatalogPreview.setSynch("0");
                            catalogPreviewService.updateSCatalogPreview(sCatalogPreview);
                        }
                    }
                    //修改被选中内容的状态
                   /* if(answering.getAnsweringList() != null && answering.getAnsweringList().size() > 0){
                        for (SAnswering answering1 : answering.getAnsweringList()) {
                            //修改状态
                            answering1.setIsStu(sAnswering.getIsStu());
                            answering1.setSynch(sAnswering.getSynch());
                            //进行修改
                            answeringService.updateSAnswering(answering1);
                        }
                    }*/
                }
                //删除中间表数据
                catalogPreviewSynService.deleteSCatalogPreviewSynByCatalogId(preview.getCatalogId());
            }else {
                //没有选择了查看内容 将状态重置为 0 0
                SCatalogPreview catalogPreview = new SCatalogPreview();
                catalogPreview.setCatalogId(preview.getCatalogId());
                List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewList(catalogPreview);
                for (SCatalogPreview sCatalogPreview : previews) {
                    sCatalogPreview.setIsStu("0");
                    sCatalogPreview.setSynch("0");
                    catalogPreviewService.updateSCatalogPreview(sCatalogPreview);
                }
            }
            //中间表添加数据
            if(preview.getAnsweringList() != null && preview.getAnsweringList().size() > 0 ){
                previewSyn.setCreateTime(new Date());
                for (SCatalogPreview preview1 : preview.getAnsweringList()) {
                    previewSyn.setPreviewId(preview1.getId());
                    catalogPreviewSynService.insertSCatalogPreviewSyn(previewSyn);
                }
            }
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }




    /**
     *  根据班级查询其部门信息
     */
    public List<TClass> findDept(List<TClass> classList){
        if(classList.size() > 0){
            for (TClass tClass : classList) {
                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                //选择在授班级时上级部门查询错误修改
                if(StringUtils.isNotNull(schooldept)){
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
            }
        }
        return classList;
    }

    /**
     *  查看班级  2
     */
    public List<TClass> answerClass2(Integer catalogId ){
        //根据查看内容 查询 查看班级
        List<TClass> classList = classService.selectTClassByCatalogIdPreviewAll(catalogId);
        List<TClass> dept = new ArrayList<>();
        if(classList != null && classList.size() > 0){
            dept = findDept(classList);
            return dept;
        }
        return dept;
    }


    /**
     *  查看内容 针对于左侧目录  1
     */
    public List<SCatalogPreview> lookContent(Integer catalogId){
        List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewByCatalogId(catalogId);
        return previews;
    }

    /** list */
    @GetMapping("/list")
    public ApiReturnObject list(SCatalogPreview catalogPreview){
        catalogPreview.setSynch("1");
        List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewList(catalogPreview);
        return new ApiReturnObject("200","查询成功",previews);
    }

    /**
     * 查询课程问答列表
     */
    @GetMapping("/listAll")
    public ApiReturnObject listAll(Integer catalogId) {
        //  查看班级字符串  查看内容字符串  是否同步  学生查看    名称拼接
        //根据当前id 查询出列表
        if(catalogId != null ){
            SCatalogPreview preview = new SCatalogPreview();
            preview.setCatalogId(catalogId);

            HashMap<String, Object> map = new HashMap<>();
            //班级字符串
            List<TClass> classList = answerClass2(catalogId);
            map.put("classList",classList);
            if(classList != null && classList.size() > 0){
                String s = "";
                int size = classList.size();
                //拼接查看班级字符串
                for (int i = 0; i < size; i++) {
                    if(i == size - 1){
                        //最后一个
                        s = s + classList.get(i).getClassName();
                    }else {
                        s = s + classList.get(i).getClassName() + ",";
                    }
                }
                map.put("classString",s);
            }else {
                map.put("classString",null);
            }
            //查看内容字符串
            List<SCatalogPreview> resList =  lookContent(catalogId);
            map.put("resList",resList);
            if(resList != null && resList.size() > 0){
                String s = "";
                int size = resList.size();
                //拼接查看班级字符串
                for (int i = 0; i < size; i++) {
                    if(i == size - 1){
                        //最后一个
                        s = s + resList.get(i).getPreviewText();
                    }else {
                        s = s + resList.get(i).getPreviewText() + ",";
                    }
                }
                map.put("resString",s);

                SCatalogPreview preview1 = resList.get(0);
                //是否同步
                if(preview1.getSynch() != null){
                    if("0".equals(preview1.getSynch())){
                        map.put("synchBool",false);
                    }else if("1".equals(preview1.getSynch())){
                        map.put("synchBool",true);
                    }
                }else {
                    map.put("synchBool",false);
                }
                //是否允许学生查看
                if(preview1.getIsStu() != null){
                    if("0".equals(preview1.getIsStu())){
                        map.put("stuBool",false);
                    }else if("1".equals(preview1.getIsStu())){
                        map.put("stuBool",true);
                    }
                }else {
                    map.put("stuBool",false);
                }
            }else {
                map.put("synchBool",false);
                map.put("stuBool",false);
                map.put("resString",null);
            }
            //资源的分类
            List<SCatalogPreview> previews = catalogPreviewService.selectSCatalogPreviewList(preview);

            map.put("answeringList",previews);
            return new ApiReturnObject("200","查询成功",map);
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }


    /**
     * 新增课程预习
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SCatalogPreview catalogPreview) {
        if(catalogPreview.getCatalogId() != null){
            //默认不让学生查看
            catalogPreview.setIsStu("0");
            //同步上课资料
            catalogPreview.setSynch("0");
            catalogPreviewService.insertSCatalogPreview(catalogPreview);
            return new ApiReturnObject("200","添加成功","");
        }
        return new ApiReturnObject("201","缺少catalogId","");
    }

    /**
     *  获取到最上面一级的id     查看班级列表 课程分享班级
     */
    public Integer GetFirstId2(Integer catalogId){
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        //第二级
        if(sCatalogMenu.getParentId() != 0){
            //第一级
            SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
            return GetFirstId2(sCatalogMenu1.getId());
        }
        return sCatalogMenu.getId();
    }

    /**
     *  修改此门备课的状态
     */
    public SCatalogMenu changeStatus(SCatalogPreview catalogPreview){
        //将资源所在的备课状态改变
        Integer rCatalogId = catalogPreview.getCatalogId();
        //教师创建课程表
        SCatalogMenu sCatalog = catalogMenuService.selectSCatalogMenuById(rCatalogId);
        //目录最下面一级
        Integer parentId = sCatalog.getParentId();
        //备课课程id
        Integer integer = GetFirstId2(parentId);
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(integer);
        return catalogMenu;
    }

    public SCatalogPreview updateStatus(SCatalogPreview catalogPreview){
        //app 端查看 允许
        String i = "1";
        if(i.equals(catalogPreview.getIsStu())){
            SCatalogMenu catalogMenu = changeStatus(catalogPreview);
            if(!i.equals(catalogMenu.getIsStu())){
                //修改备课是否允许学生查看状态
                catalogMenu.setIsStu("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        // 同步到上课资料
        if(i.equals(catalogPreview.getSynch())){
            //同步上课资料
            SCatalogMenu catalogMenu = changeStatus(catalogPreview);
            if(!i.equals(catalogMenu.getIsSyn())){
                //修改同步上课资料状态
                catalogMenu.setIsSyn("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        return catalogPreview;
    }


    /**
     * 修改课程预习
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SCatalogPreview catalogPreview) {
//        if(catalogPreview.getId() != null){
            //修改某个字段 避免空值报错 针对于查看内容中的课程问答状态
            //修改 查看内容 状态  修改上级
            if(catalogPreview.getCatalogId() != null){
                updateStatus(catalogPreview);
            }
            if(catalogPreview.getContentList() != null && catalogPreview.getContentList().size() >0 ){
                for (SCatalogPreview catalogPreview1 : catalogPreview.getContentList()) {
                    if(catalogPreview.getIsStu() != null && !"".equals(catalogPreview.getIsStu())){
                        catalogPreview1.setIsStu(catalogPreview.getIsStu());
                    }
                    if(catalogPreview.getSynch() != null && !"".equals(catalogPreview.getSynch())){
                        catalogPreview1.setSynch(catalogPreview.getSynch());
                    }
                    catalogPreviewService.updateSCatalogPreview(catalogPreview1);
                }
                return new ApiReturnObject("200","修改成功","");
            }else {
                return new ApiReturnObject("202","请选择要修改的内容","");
            }
//        }
//        return new ApiReturnObject("201","缺少id","");
    }

    /**
     * 修改课程问答
     */
    @PostMapping("/updateData")
    public ApiReturnObject updateData(@RequestBody SCatalogPreview catalogPreview) {
        if(catalogPreview.getId() != null){
            //修改是否允许学生查看    是否同步上课资料
            catalogPreviewService.updateSCatalogPreview(catalogPreview);
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }

    /**
     * 删除课程预习
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody SCatalogPreview catalogPreview) {
        if(catalogPreview.getId() != null){
            //查询该内容是否要分享
            SCatalogPreviewSyn previewSyn = new SCatalogPreviewSyn();
            previewSyn.setPreviewId(catalogPreview.getId());
            List<SCatalogPreviewSyn> sCatalogPreviewSyns = catalogPreviewSynService.selectSCatalogPreviewSynList(previewSyn);
            if(sCatalogPreviewSyns != null && sCatalogPreviewSyns.size() > 0 ){
                return new ApiReturnObject("201","该资源已分享","");
            }
            catalogPreviewService.deleteSCatalogPreviewById(catalogPreview.getId());
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }


}